--兴炉起灶
local xingluqizao = fk.CreateSkill {
    name = "mcdh_xingluqizao",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

xingluqizao:addEffect('active',{
    can_use = function (self, player, card, extra_data)
        return player:getMaxCards() ~= #player:getCardIds(Player.Hand) and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
    end,
    card_filter = function (self, player,to_select, selected)
        return #selected < math.max(0, #player:getCardIds(Player.Hand) - player:getMaxCards()) and Fk:currentRoom():getCardArea(to_select) == Player.Hand
    end,
    target_filter = function (self, player,to_select, selected)
        return #selected < math.max(0, #player:getCardIds(Player.Hand) - player:getMaxCards())
    end,
    feasible = function (self, player,selected, selected_cards)
        return (#selected == 0 or #selected == math.max(0, #player:getCardIds(Player.Hand) - player:getMaxCards())
                and #selected_cards == math.max(0, #player:getCardIds(Player.Hand) - player:getMaxCards()))
    end,
    prompt = function (self,player, selected_cards, selected_targets)
        if #player:getCardIds(Player.Hand) < player:getMaxCards() then
            local num = player:getMaxCards() - #player:getCardIds(Player.Hand)
            return "#mcdh_xingluqizao_draw:::"..num
        else
            local num = #player:getCardIds(Player.Hand) - player:getMaxCards()
            return "#mcdh_xingluqizao_discard:::"..num
        end
    end,
    on_use = function (self, room, use)
        local player = use.from
        if #use.cards > 0 then
            room:throwCard(use.cards, self.name, player, player)
            local tos = use.tos
            room:sortByAction(tos)
            for _, t in ipairs(tos) do
                t:drawCards(1,self.name)
            end
        else
            player:drawCards(player:getMaxCards() - #player:getCardIds(Player.Hand),self.name)
        end
    end
})

Fk:loadTranslationTable {
    ["mcdh_xingluqizao"] = "起灶",
    [":mcdh_xingluqizao"] = "『兴炉起灶』<br>出牌阶段限一次，你可以将手牌调整至手牌上限；当你以此法弃置牌时，你可以令等量名角色各摸一张牌。",
    ["#mcdh_xingluqizao_draw"] = "兴炉起灶：你可以将手牌摸至手牌上限（摸 %arg 张牌）",
    ["#mcdh_xingluqizao_discard"] = "兴炉起灶：你可以将手牌弃至手牌上限（弃 %arg 张牌），并可以选择 %arg 名角色，令这些角色各摸一张牌"
}

return xingluqizao